import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
const Home = require("@/views/Home").default;
const Echarts = require("@/views/Echarts").default;
const NotFound = require("@/views/error/404.vue").default;
const NotAllow = require("@/views/error/403.vue").default;

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    meta: {
      title: "首页",
      allow: true,
    },
  },
  {
    path: "/echarts",
    name: "Echarts",
    component: Echarts,
    meta: {
      title: "echarts",
      allow: false,
    },
  },

  {
    path: "/403",
    name: "NotAllow",
    component: NotAllow,
    meta: {
      title: "403",
      allow: true,
    },
  },
  {
    path: "*",
    name: "404",
    component: NotFound,
    meta: {
      title: "404",
      allow: true,
    },
  },
];

const router = new VueRouter({
  mode: "history",
  routes,
});

router.beforeEach((to, from, next) => {
  NProgress.start();
  to.meta.allow?next():next("/403");
    next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
